 /*
  * Powered By zsCat, Since 2014 - 2020
  */
package com.zscat.order.serviceImpl;

import java.math.BigDecimal;
import java.util.Date;

import javax.annotation.Resource;

import com.alibaba.dubbo.config.annotation.DubboConsumer;
import com.alibaba.dubbo.config.annotation.DubboService;
import com.zscat.goods.service.BlogService;
import com.zscat.goods.service.CartService;
import com.zscat.goods.service.GoodsOrderService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.zscat.common.base.ServiceMybatis;
import com.zscat.common.utils.RandomString;

import com.zscat.order.mapper.OrderLogMapper;
import com.zscat.order.mapper.OrderMapper;
import com.zscat.goods.model.Cart;
import com.zscat.goods.model.GoodsOrder;
import com.zscat.order.model.Order;
import com.zscat.order.model.OrderLog;
import com.zscat.order.service.OrderService;


/**
 * 
 * @author zsCat 2017-4-14 13:56:18
 * @Email: 951449465@qq.com
 * @version 4.0v
 */
@Component
@DubboService(interfaceClass = OrderService.class,timeout =6000 )
public class OrderServiceImpl extends ServiceMybatis<Order> implements OrderService {

	@Resource
	private OrderMapper OrderMapper;
	@Resource
	private OrderLogMapper  OrderLogMapper;
	@DubboConsumer
	private GoodsOrderService GoodsOrderServer;
	@DubboConsumer
	private CartService CartService;


	@Override
	public Order insertOrder(String[] cartIds,Long addressid, Long paymentid, String usercontent,Long uid,String uname) {
		Order order=new Order();
		if(cartIds!=null && cartIds.length>0){
			int count=0;
			BigDecimal total=BigDecimal.ZERO;
			for(String cartId:cartIds){
				Cart cart=CartService.selectByPrimaryKey(Long.parseLong(cartId));
				if(cart==null){
					return null;
				}
				count+=cart.getCount();
				total =total.add(BigDecimal.valueOf(Double.valueOf(cart.getPrice())).multiply(BigDecimal.valueOf(cart.getCount())));
				GoodsOrder go=new GoodsOrder();
				go.setGoodsid(cart.getGoodsid());
				go.setOrderid(order.getId());
				GoodsOrderServer.insertSelective(go);
				CartService.deleteByPrimaryKey(cart);
			}
			order.setOrdersn(RandomString.generateRandomString(8));
			order.setCreatedate(new Date());
			order.setStatus(1);
			order.setUserid(uid);
			order.setUsername(uname);
			//	order.setPaymentid(paymentid);
			order.setUsercontent(usercontent);
			order.setAddressid(addressid);
			//order.setOrderTotalPrice();
			OrderMapper.insertSelective(order);

			OrderLog log=new OrderLog();
			log.setOrderId(order.getId());
			log.setOrderState("1");
			log.setStateInfo("提交订单");
			log.setCreateTime(new Date().getTime());
			log.setOperator(uname);
			//	OrderLogMapper.insertSelective(log);

			order.setTotalcount(count);
			order.setTotalprice(total);
			OrderMapper.updateByPrimaryKey(order);
		}
		return order;

	}

 
    
}
